﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

namespace LabMS.Web.BaseData
{
    public partial class BaseDataStudentList : Base.BasePage
    {
        ExtendBLL.Teacher ET = new ExtendBLL.Teacher();
        ExtendBLL.Student EStudent = new ExtendBLL.Student();

        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("XSSJ");
            strPageUserType.Add("mana");

            if (!IsPostBack)
            {
                InitBind();
            }
            btnDel.Attributes.Add("onclick", "return confirm('你确定要删除？');");
        }

        protected void InitBind()
        {
            DataSet ds = new DataSet();
            ds = EStudent.GetAllList();
            HandleDS(ds);
            gvStudentList.DataSource = ds.Tables[0].DefaultView;
            gvStudentList.DataBind();
        }

        /// <summary>
        /// 处理绑定数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        protected DataSet HandleDS(DataSet ds)
        {
            //构造: 性别Sex 班级名称ClassName 专业名称ProName 三列
            ds.Tables[0].Columns.Add("Sex");
            ds.Tables[0].Columns.Add("ClassName");
            ds.Tables[0].Columns.Add("ProName");
            ExtendBLL.Dictionary EDictionary = new ExtendBLL.Dictionary();
            ExtendBLL.Class EClass = new ExtendBLL.Class();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string Student_Sex = ds.Tables[0].Rows[i]["Student_Sex"].ToString();
                if (!string.IsNullOrEmpty(Student_Sex))
                {
                    string str = "DClass='201' and Code='" + Student_Sex + "'";
                    ds.Tables[0].Rows[i]["Sex"] = EDictionary.GetModelList(str)[0].Name.ToString();
                }
                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["ClassID"].ToString()))
                {
                    int ClassID = int.Parse(ds.Tables[0].Rows[i]["ClassID"].ToString());
                    ds.Tables[0].Rows[i]["ClassName"] = EClass.GetClassNameByClassID(ClassID);
                }
                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Student_Pro"].ToString()))
                {
                    int Student_Pro = int.Parse(ds.Tables[0].Rows[i]["Student_Pro"].ToString());
                    ds.Tables[0].Rows[i]["ProName"] = EClass.ProessionalNameByID(Student_Pro);
                }
            }
            return ds;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            bool success = false;
            CheckBox cb;
            for (int i = 0; i < gvStudentList.Rows.Count; i++)
            {
                cb = (CheckBox)gvStudentList.Rows[i].FindControl("Chk");
                if (cb.Checked)
                {
                    int ID = int.Parse(gvStudentList.DataKeys[i].Value.ToString());
                    try
                    {
                        EStudent.Delete(ID);
                        success = true;
                    }
                    catch
                    {
                        Common.JShelper.JSAlert(this.Page, "", "该学生和其它模块有联系，不能删除！");
                    }
                }
            }
            if (success == true)
            {
                Common.JShelper.JSAlert(this.Page, "", "删除成功！");
                InitBind();
            }
            else
            {
                Common.JShelper.JSAlert(this.Page, "", "删除失败！");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            tbSearchClassID.Text = "";
            tbSearchClassName.Text = "";
            tbSearchStudent_Grade.Text = "";
            tbSearchStudent_Pro.Text = "";
        }

        protected void gvStudentList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStudentList.PageIndex = e.NewPageIndex;
            InitBind();
        }

        protected void gvStudentList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }
    }
}
